<?php

namespace app\uniapp\service;

use app\model\store\Coupon;
use app\model\store\UserCoupon;
use app\uniapp\service\traits\InstanceTrait;
use think\db\Query;

class CouponService extends UniBaseService
{
    use InstanceTrait;

    function initialize()
    {
        $this->model = new Coupon();
    }

    function formatData(array $data): array
    {
        return $data;
    }

    public function query(): Query
    {
        return $this->makeQuery()->where('is_show',1)->filter(function ($v){
            // 判断当前登录的用户是否领取
            $get = UserCoupon::where('coupon_id', $v->id)->where('user_id', $this->userId)->find();
            $v->isGet = (bool)$get;
        });
    }
}
